#!/usr/bin/env python

import os, sys
from newsletter_class import Newsletter

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
    res = {}

    count = 0
    for v in rows:
        v [ "_pos" ] = start_point + count
        res [ "row%d" % count ] = v
        count += 1

    res [ 'rows' ] = tot_rows
    res [ 'from_row' ] = start_point
    res [ 'to_row' ] = start_point + count
    res [ 'lines' ] = lines

    return res
#}}}
#{{{def del_subscriber ( liwe, uids = "" ):
def del_subscriber ( liwe, ids = "" ):
	"""
	INPUT:
		-ids: SAFE_DB_STRING
	:PERMS:
		- delete With this permission you can delete 
	"""
	n = Newsletter ( liwe )
	resp =  n.del_subscriber ( ids =  ids )
#}}}
#{{{def set_info ( liwe, id_info = 0, title = '', sender = '', subject = '', template = '',  send_time = '' ):
def set_info ( liwe, id_info = 0, title = '', sender = '', subject = '', template = '', send_time = '' ):
	"""
	:INPUT:
		- id_info:    SAFE_NUMBER
		- title:    SAFE_DB_STRING
		- sender:   SAFE_DB_STRING
		- subject:  SAFE_HTML
		- template:  SAFE_HTML
		- send_time:    SAFE_DB_STRING
	:PERMS:
		- create:   With this permission you can create/modify
	"""
	n = Newsletter ( liwe )
	resp = n.set_info ( id_info = id_info, title = title, sender = sender, subject = subject, template = template, send_time = send_time )
	return { 'info' : resp }

#}}}
#{{{def del_info ( liwe, uids = "" ):
def del_info ( liwe, ids = "" ):
    """
    :INPUT:
        - ids: SAFE_DB_STRING
    :PERMS:
        - delete With this permission you can delete 
    """
    n = Newsletter ( liwe )
    resp =  n.del_info ( ids =  ids )
#}}}
#{{{check_info_rel ( liwe, id_info = 0 ):
def check_info_rel ( liwe, id_info = 0 , info_only = 0 , id_subscriber = 0):
	"""
	:INPUT:
		- id_info: SAFE_NUMBER
		- info_only : SAFE_NUMBER
		- id_subscriber	: SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	return { 'check' : n.check_info_rel ( id_info =  id_info, info_only = info_only, id_subscriber = id_subscriber ) }
#}}}
#{{{set_info_rel ( liwe, id_info = 0, rels = '' ):
def set_info_rel ( liwe, id_info = 0, rels = '' ):
	"""
	:INPUT:
		- id_info:	SAFE_NUMBER
		- rels:				SAFE_DB_STRING
	"""
	n = Newsletter ( liwe )
	resp = n.set_info_rel ( id_info = id_info, rels =  rels )
	return { 'rels' : resp }
#}}}
#{{{def set_sent ( liwe, id_sent = 0, id_newsletter = 0, html = '', sent = , created =  ):
def set_sent ( liwe, id_sent = 0, id_info = 0, subject= '', html = '', sent = '' , kwargs = '' ):
	"""
	:INPUT:
		- id_sent:    SAFE_NUMBER
		- id_info:    SAFE_NUMBER
		- subject: SAFE_STRING
		- html: SAFE_STRING
		- sent: SAFE_DATE
	:PERMS:
		- create:   With this permission you can create/modify
	"""
	n = Newsletter ( liwe )
	resp = n.set_sent ( id_sent = id_sent, id_info = id_info, subject = subject, html = html, sent = sent, kwargs = kwargs )
	return { 'sent' : resp }

#}}}
#{{{def del_sent ( liwe, uids = "" ):
def del_sent ( liwe, ids = "" ):
	"""
	:INPUT:
		-ids: SAFE_DB_STRING
	:PERMS:
		- delete With this permission you can delete
	"""
	n = Newsletter ( liwe )
	resp =  n.del_sent ( ids =  ids ) 
#}}}
#{{{def get_sent ( liwe, id_table.name = 0 ):
def get_sent ( liwe, id_sent = 0 ):
	"""
	:INPUT:
		- id_sent:	SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	return { "sent" : n.get_sent ( id_sent = id_sent ) }
#}}}
#{{{def list_sent ( liwe, id_newsletter = 0, quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_sent ( liwe, id_info = 0, quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_info:    SAFE_NUMBER
		- quant:			SAFE_NUMBER
		- _X_LINES:			SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:			SAFE_NUMBER
	"""

	use_dataset = 0

	if _X_LINES != -1:
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	n = Newsletter ( liwe )
	resp = n.list_sent ( id_info = id_info, quant = quant, start_point = start_point, do_count = use_dataset )

	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset:
		return { 'sent': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}

#{{{def set_group ( liwe, id_group = 0, title = '' ):
def set_group ( liwe, id_group = 0, title = '' ):
	"""
	:INPUT:
		- id_group:    SAFE_NUMBER
		- title:    SAFE_DB_STRING
	:PERMS:
		- create:   With this permission you can create/modify
	"""
	n = Newsletter ( liwe )
	resp = n.set_group ( id_group = id_group, title = title )
	return { 'group' : resp }
#}}}

#{{{def del_group ( liwe, uids = "" ):
def del_group ( liwe, ids = "" ):
    """
    :INPUT:
        - ids: SAFE_DB_STRING
    :PERMS:
        - delete With this permission you can delete group
	"""
    n = Newsletter ( liwe )
    resp =  n.del_group ( ids =  ids )
#}}}
def subscriber_import ( liwe, id_group = 0, new_group = '' ):
	"""
	:INPUT:
		- id_group: SAFE_NUMBER
		- new_group: SAFE_DB_STRING
	:PERMS:
		- import: With this permissione you can import csv
	"""
	n = Newsletter ( liwe )
	resp = n.subscriber_import ( id_group = id_group, new_group = new_group );
	return { "import" : resp }




